package com.ruoyi.fileMaintenance.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.fileMaintenance.domain.TCourseBinding;
import com.ruoyi.fileMaintenance.domain.TCourseLearning;
import com.ruoyi.fileMaintenance.domain.dto.TCourseDto;
import org.springframework.stereotype.Service;
import com.ruoyi.fileMaintenance.mapper.TCourseMapper;
import com.ruoyi.fileMaintenance.domain.TCourse;
import com.ruoyi.fileMaintenance.service.ITCourseService;

import javax.annotation.Resource;

/**
 * 课程Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-27
 */
@Service
public class TCourseServiceImpl implements ITCourseService 
{
    @Resource
    private TCourseMapper tCourseMapper;

    /**
     * 查询课程
     * 
     * @param id 课程主键
     * @return 课程
     */
    @Override
    public TCourse selectTCourseById(Long id)
    {
        return tCourseMapper.selectTCourseById(id);
    }

    /**
     * 查询课程列表
     * 
     * @param tCourse 课程
     * @return 课程
     */
    @Override
    public List<TCourse> selectTCourseList(TCourse tCourse)
    {
        return tCourseMapper.selectTCourseList(tCourse);
    }

    /**
     * app查询课程列表
     *
     * @return 课程集合
     */
    @Override
    public List<TCourseDto> selectTCourseList2(TCourseDto tCourseDto) {
        tCourseDto.setUserId(SecurityUtils.getUserId());
        return tCourseMapper.selectTCourseList2(tCourseDto);
    }

    /**
     * 查询用户绑定的课程
     *
     * @return 课程集合
     */
    @Override
    public TCourseBinding selectCourseBinding() {
        TCourseBinding tCourseBinding = new TCourseBinding();
        tCourseBinding.setUserId(SecurityUtils.getUserId());
        tCourseBinding = tCourseMapper.selectCourseBinding(tCourseBinding);
        if (tCourseBinding != null) {
            tCourseBinding.setNotLearned(tCourseBinding.getTotalNum() - tCourseBinding.getLearned());
        }
        return tCourseBinding;
    }

    /**
     * 新增课程
     * 
     * @param tCourse 课程
     * @return 结果
     */
    @Override
    public int insertTCourse(TCourse tCourse)
    {
        tCourse.setCreateBy(SecurityUtils.getUsername());
        tCourse.setCreateTime(DateUtils.getNowDate());
        return tCourseMapper.insertTCourse(tCourse);
    }

    /**
     * 修改课程
     * 
     * @param tCourse 课程
     * @return 结果
     */
    @Override
    public int updateTCourse(TCourse tCourse)
    {
        tCourse.setUpdateBy(SecurityUtils.getUsername());
        tCourse.setUpdateTime(DateUtils.getNowDate());
        return tCourseMapper.updateTCourse(tCourse);
    }

    /**
     * 批量删除课程
     * 
     * @param ids 需要删除的课程主键
     * @return 结果
     */
    @Override
    public int deleteTCourseByIds(Long[] ids)
    {
        return tCourseMapper.deleteTCourseByIds(ids);
    }

    /**
     *  课程绑定
     * @author
     * @param courseId 课程id
     * @return 结果
     */
    @Override
    public int insertCourseBinding(Long courseId) {
        // 删除原有绑定课程
        tCourseMapper.deleteCourseBinding(SecurityUtils.getUserId());
        // 新增课程绑定数据
        TCourseBinding tCourseBinding = new TCourseBinding();
        tCourseBinding.setCourseId(courseId);
        tCourseBinding.setUserId(SecurityUtils.getUserId());
        tCourseBinding.setCreateBy(SecurityUtils.getUsername());
        tCourseBinding.setCreateTime(DateUtils.getNowDate());
        return tCourseMapper.insertCourseBinding(tCourseBinding);
    }

    /**
     *  课程重新学习
     * @author
     * @param courseId 课程id
     * @return 结果
     */
    @Override
    public int courseRelearn(Long courseId) {
        tCourseMapper.deleteTCourseLearning(courseId, SecurityUtils.getUserId());
        return 1;
    }
}
